﻿using static Microsoft.Extensions.Logging.EventSource.LoggingEventSource;

namespace CMS.DataAccess
{
    public class WhereCond
    {
        public WhereCond(string field)
        {
            this.FieldName = field;
            this.Op =  OpEnum.Eq;
        }
        public WhereCond(string field, OpEnum op, object value)
        { 
            this.FieldName = field;
            this.Op = op;
            this.Value = value;
        }
        public WhereCond(string field, OpEnum op)
        {
            this.FieldName = field;
            this.Op = op;
            this.Value = null;
        }
        public string FieldName { get; set; }
        public object? Value { get; set; }
        public OpEnum Op { get; set; }
        public string SqlParamName { get { return $"@{FieldName}"; } }

        public string ToSql(int counter)
        {
            switch (Op)
            {
                case OpEnum.Eq:
                    return $" {FieldName} = {SqlParamName}{counter} ";
                case OpEnum.EqIgnoreCase:
                    return $" UPPER({FieldName}) = UPPER({SqlParamName}{counter}) ";
                case OpEnum.NotEq:
                    return $"{FieldName} != {SqlParamName}{counter} ";
                case OpEnum.Lk:
                    return $"{FieldName} LIKE {SqlParamName}{counter} ";
                case OpEnum.Gt:
                    return $"{FieldName} > {SqlParamName}{counter} ";
                case OpEnum.Ge:
                    return $"{FieldName} >= {SqlParamName}{counter} ";
                case OpEnum.Lt:
                    return $"{FieldName} < {SqlParamName}{counter} ";
                case OpEnum.Le:
                    return $"{FieldName} <= {SqlParamName}{counter} ";
                case OpEnum.IsNull:
                    return $"{FieldName} IS NULL ";
                case OpEnum.IsNotNull:
                    return $"{FieldName} IS NOT NULL ";
                default:
                    return "";
            }
        }
    }
    public enum OpEnum
    {
        Eq, EqIgnoreCase, NotEq, Lk, Gt, Lt, Ge, Le, IsNull, IsNotNull
    }
}
